<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class AppUserBaseC extends AppBaseC {

    protected $uid = 0;
    protected $userInfo = array();
    protected $cacheTime = 300; //缓存时间
    public function init() {
        parent::init();
        $token = Yii::app()->request->getParam('token');
        if (empty($token)) {
            $this->ajaxReturn(array('info' => '请先登录！','error_code'=>1013));
        }
        $this->uid = MembersAccessModel::model()->getUid($token);
        if ($this->uid <= 0) {
            $this->ajaxReturn(array('info' => '请先登录！','error_code'=>1014));
        }
        //验证通过后处理 
        $this->getUserInfo();
    }

    /**
     * 获取用户信息
     * @param type $isCache true使用缓存，false删除缓存
     * @return boolean
     */
    protected function getUserInfo($isCache = true) {
        $key = 'AppUserBaseC_getUserInfo_' . $this->uid;
        if($isCache === false){
            Yii::app()->cache->delete($key);
        }
        $userInfo = Yii::app()->cache->get($key);
        if (empty($userInfo)) {
            $userInfoObj = MembersModel::model()->findByPk($this->uid);
            $userInfo = $userInfoObj->attributes;
            Yii::app()->cache->set($key, $userInfo, $this->cacheTime);
        }
        if (empty($userInfo)) {
            return false;
        }
        $this->userInfo = $userInfo;
        return $userInfo;
    }
}
